package backstage.app.controller;

import java.time.LocalDateTime;
 
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
 
import backstage.app.entity.StudentType;
import backstage.app.mapper.StudentMapper;
import backstage.app.mapper.StudentTypeMapper;
import backstage.app.respont.RespontDateJson;
import backstage.app.service.StudentTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
@RestController
@RequestMapping("${spring.application.name}/studentType/api")
@Api("学员类型-控制器")
public class StudentTypeController {
	
	@Autowired
	private StudentTypeService studentTypeService;
	
	@Autowired
	private StudentTypeMapper studentTypeMapper;
	
	@Autowired
	private StudentMapper studentMapper;
	
	@PostMapping("/list")
	@ApiOperation(value="类型", notes="")
	public List<StudentType> list() {
		List<StudentType>listStudenttype = null;
		
			 listStudenttype=studentTypeMapper.selectList(null); 
			 for (StudentType studentType : listStudenttype) { 
				 if(null!=studentType.getStudentType()) {
					 Integer total=studentMapper.selectByPrimaryKeyCountType(studentType.getStudentType());
					 studentType.setTotal(total);
				 }
				 
			 }
				  
			 return  listStudenttype;
		
	 
	}
	
	
	@PostMapping("/inser")
	@ApiOperation(value="添加标签")
	public RespontDateJson<?>inser(@RequestParam("name")String name){
		
		StudentType studenttype=studentTypeMapper.selectByNames(name);
		if(studenttype!=null) {
			return RespontDateJson.ok(studenttype, "此标签"+name+"已经存在数据库中了");
		}
		studenttype=new StudentType();
		studenttype.setNames(name);
		studenttype.setCreateTime(LocalDateTime.now());
		int colum=studentTypeMapper.insert(studenttype);
		int result=-1;
		if(colum>0) {
			studenttype.setStudentType(studenttype.getId());
			result=studentTypeService.updateByPrimaryKey(studenttype);
		}
		 
			
		return RespontDateJson.ok(result, "便签添加成功");
		
		 
	}
	
	
	@PostMapping("/delete")
	@ApiOperation("删除学员标签")
	public RespontDateJson<?>delete(@RequestParam(value="id",required = true)Integer id){
		int index=studentTypeService.deleteByPrimaryKey(id);
		return  index>0?RespontDateJson.ok(index, "便签删除成功"):RespontDateJson.ok(index, "便签删除失败");
	}
	
	
}
